import { _decorator, clamp, Component, log, Node } from 'cc';
import { skillType } from '../Data/SkillData';
import { Battle } from '../Battle';
import { Player } from '../Player/Player';
const { ccclass, property } = _decorator;
// 技能选项卡的选项的组件
@ccclass('SkillPanelItem')
export class SkillPanelItem extends Component {

    public skillData: skillType = null; // 本选项的技能数据
    private _playerSkillList: skillType[] = []; // 玩家技能栏

    onLoad(): void {
        this.node.on(Node.EventType.TOUCH_START, this._onTouchStart, this);
        this.node.on(Node.EventType.TOUCH_END, this._onTouchEnd, this);
    }
    onEnable(): void {
        // 获取玩家技能栏数据
        this._playerSkillList = Battle.playerNode.getComponent(Player).skillList;
    }
    onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this._onTouchStart, this);
        this.node.off(Node.EventType.TOUCH_END, this._onTouchEnd, this);
    }
    start() {
    }


    update(deltaTime: number) {

    }

    private _onTouchStart() {
        this.node.setScale(.9, .9);
    }
    private _onTouchEnd() {
        this.node.setScale(1, 1);
        // 关闭技能面板
        this.node.getParent().getParent().active = false;
        // 把技能存入技能栏，如果技能已经存在就升级
        // 判断技能是否已经存在
        let hasSkill = false;
        this._playerSkillList.forEach(skill => {
            if (skill.id == this.skillData.id) {
                hasSkill = true;
                return;
            }
        })
        hasSkill ? this.upPlaySkill() : this.setPlayerSkill();

    }

    // 存入技能
    setPlayerSkill() {
        log("存入技能", this._playerSkillList);
        log("技能", this.skillData);
        this._playerSkillList.push(this.skillData);
        // 判断技能类型
        switch (this.skillData.type) {
            case "passive":
                // 被动技能
                break;
            case "active":
                // 主动技能
                break;
            default: break;
        }
    }
    // 升级技能
    upPlaySkill() {
        this._playerSkillList.forEach((skill, index) => {
            if (skill.id == this.skillData.id) {
                // 升级技能, 技能组件里面有自己更新
                //因为对象与数组的直接赋值，是引用赋值，所以这里的skillData改变（技能等级升级），传递给技能节点（PassiveItem）的skillData也会改变
                this._playerSkillList[index].level++;
                // 冷却时间减少
                this._playerSkillList[index].coolingTime = clamp(this._playerSkillList[index].coolingTime - (this._playerSkillList[index].level * .05), 1, 999);
                log("升级技能", this._playerSkillList[index]);
            }
        })
    }
}


